package dao

import javax.inject.Inject
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.{ExecutionContext, Future}

class GenomeDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
                         (implicit exec: ExecutionContext) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insert(row: Seq[GenomeRow]): Future[Unit] = {
    db.run(Genome ++= row).map(_ => ())
  }

  def getAllGenome: Future[Seq[GenomeRow]] = {
    db.run(Genome.result)
  }

  def getById(id: Int): Future[GenomeRow] = {
    db.run(Genome.filter(_.id === id).result.head)
  }

  def getByGeneId(geneId: String): Future[Option[GenomeRow]] = {
    db.run(Genome.filter(_.geneid === geneId).result.headOption)
  }

  def getByGeneIds(geneIds: Seq[String]): Future[Seq[GenomeRow]] = {
    db.run(Genome.filter(_.geneid.inSetBind(geneIds)).result)
  }

  def getByCon(species: String, rna: String, tls: String, snp: String, m5c: String, m6a: String): Future[Seq[GenomeRow]] = {

    val first = Genome.filter(_.species === species).filter(_.rna === rna).filter(_.tls === tls).filter(_.snp === snp)
    val second = m5c match {
      case "m5g" => first.filter(_.m5g =!= "no")
      case "m5p" => first.filter(_.m5p =!= "no")
      case "no" => first.filter(_.m5g === "no").filter(_.m5p === "no")
    }
    val last = m6a match {
      case "m6g" => second.filter(_.m6g =!= "no")
      case "m6p" => second.filter(_.m6p =!= "no")
      case "no" => second.filter(_.m6g === "no").filter(_.m6p === "no")
    }
    db.run(last.result)
  }


}
